/**
 * @param {number[]} nums
 * @return {number}
 */
 var minSwaps = function(nums) {
  const n = nums.length
  const sum = new Array(n+1).fill(0)
  for(let i = 1; i<=n; i++){
    sum[i] = sum[i-1] + nums[i-1]
  }
  let ans = Infinity
  let cnt = 0
  for(let x of nums) if(x) cnt++
  for(let i = 0; i<n; i++){
    let j = i + cnt
    let now = 0
    if(j >= n){
      now = sum[n] - sum[i] + sum[j%n]
    }else{
      now = sum[j] - sum[i]
    }
    ans = Math.min(ans, cnt -now)
  }
  return ans
};